MultiChoice = function (string) {
	"use strict";
	var seed = {};
		
	seed.id = getUniqueId();
	var sidebar = $('body').find('.side-bar .middle'),
		preview = $('body').find('.preview'),
		doc = $(document);
	
	seed.name = "";
	seed.qtext = "";
	seed.feedback = "";
	seed.fraction = [];
	seed.atexts = [];
	seed.type = "mc";
	
	seed.genXML = function () {
		var self = this;
		var mcstring = "";
		//write category
		//mcstring = "\t<question type=\"category\">\n\t\t<category><text>$course$/" + self.category + "</text>\n\t</category>\n</question>"
		
		//write question
		mcstring += "<question type=\"multichoice\">\n\t<name>\n\t\t<text><![CDATA[<p>" + self.name + "</p>]]></text>\n\t</name>\n\t<questiontext format=\"html\">\n\t\t<text><![CDATA[<p>" + self.qtext + "<p>]]> </text>\n\t";
		mcstring += "</questiontext>";
		console.log(self.atexts.length);
		for(var i = 0; i < self.atexts.length; i++) {
			mcstring += "\n\t<answer format=\"html\" fraction=\"" + self.fraction[i] + "\">\n\t\t<text>" + self.atexts[i] + "</text>\n\t</answer>";
		}
		mcstring += "\n\t<generalfeedback><text><![CDATA[<p>" + self.feedback + "</p>]]></text></generalfeedback>\n\t<shuffleanswers>1</shuffleanswers>\n\t<answernumbering>ABCD</answernumbering>\n</question>";
		return mcstring;
	};
	
	seed.genGift = function () {
		var self = this;
		var giftString = "";
		//write categories
		giftString = "$CATEGORY: " + self.category;
		
		//write question		
		giftString += "::" + self.name + "::" + self.qtext + "{";
		
		for(var i = 0; i < self.atexts.length; i++) {
			giftString += "~%" + self.fraction[i] + "%" + self.atexts[i] + "#" + self.feedback + " ";
		}
		giftString += "}\n\n";
		return giftString;
	};
	
	seed.genQSD = function () {
		var self = this;
		var qsdString = "MC:\r\n";
		
		qsdString += self.name + "; " + self.qtext + "; ";
		for(var i = 0; i < self.atexts.length; i++) {
			qsdString = qsdString + self.atexts[i] + "-" + self.fraction[i] + "; ";
		}
		qsdString += self.feedback + "\r\n\r\n";
		return qsdString;
	};
	
	seed.validate = function () {
		var self = this,
			error = "";
		
		var numAnswers = 5;
		for (var index = self.atexts.length - 1; index >= 0; index--) {
			if(self.atexts[index] === "") {
				numAnswers--;
			}
		}
		
		if(self.name === "") {
			error += "Missing field(s)";
		} else if(self.qtext === "") {
			error += "Missing field(s)";
		}
		
		for (var index = self.fraction.length - 1; index >= 0; index--) {
			if(!/^([0-9]?[0-9]|100)$/.test(self.fraction[index])) {
				if(error !== "") {
					error += ", ";
				}
				error += "Answer " + index + ": Fraction must be between 0 and 100";
			}
		}
		
		if(numAnswers < 2) {
			if(error !== "") {
				error += ", ";
			}
			error += "There must be at least two answer texts";
		}
		
		return error;
	};
	
	seed.set = function (options) {
		var self = this;
		$.each(options, function (key, value) {
			if (key === 'name') {
				self.name = value;
			} else if (key === 'qtext') {
				self.qtext = value;
			} else if (key === 'feedback') {
				self.feedback = value;
			} else if (key === 'fraction') {
				self.fraction = value;
			} else if (key === 'atexts') {
				self.atexts = value;
			}
		});
	};
		
	if (string) {
		// spilt the string up by line
		var self = seed,
			lines = string.split("\r\n"),
			currentLine = 1;
			
		if (lines.length === 1) {
			lines = string.split("\n");
		}
		
		// set up the options object
		var obj = {};
		obj.name = "";
		obj.qtext = "";
		obj.atexts = [];
		obj.fraction = [];
		obj.feedback = "";
		obj.category = "";
		
		lines[1] = lines[1].replace("\\;", ";\\");
		var data = lines[1].split(/;(?!\\)/);
		$.each(data, function(index) {
			data[index] = data[index].replace(";\\", "\\;");
		});
		
		
		// go through the lines with array data
		// trim is to get rid of leading and trailing whitespace
		while (currentLine < lines.length-1) {
			var line = lines[currentLine];
			obj.name = data[0].trim();
			obj.qtext = data[1].trim();
						
			if (!(data.length === 1 && data[0] === "")) {
				console.log(data);
				for(var i = 2; i < data.length-1; i++) {
					var index = data[i].lastIndexOf('-');
					var temp = new Array(data[i].substring(0, index), data[i].substring(index + 1));
					obj.atexts.push(temp[0].trim());
					obj.fraction.push(temp[1].trim());
				}
				obj.feedback = data[data.length-1].trim();
			}
			currentLine++;
		}
		
		seed.set(obj);
	
	};
	
	return seed;
};
